﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SlideshowGenerator
{
    public partial class Frm_Configuration : Form
    {
        private bool _saved = true;
        private bool _isLoading = true;

        public Frm_Configuration()
        {
            InitializeComponent();
        }

        private void btn_reset_Click(object sender, EventArgs e)
        {
            ResetConfiguration();
        }

        /// <summary>
        /// Sets textboxes to configuration default values.
        /// </summary>
        private void ResetConfiguration()
        {
            this.tbx_capImgFreq.Text = Configuration.DefaultValues.CAPTION_IMAGE_FREQUENCY.ToString();
            this.tbx_imgWidth.Text = Configuration.DefaultValues.IMAGE_WIDTH.ToString();
            this.tbx_imgHeight.Text = Configuration.DefaultValues.IMAGE_HEIGHT.ToString();
            this.cbx_useOriginals.Checked = Configuration.DefaultValues.USE_ORIGINAL_SIZES;
        }

        private void btn_save_Click(object sender, EventArgs e)
        {
            SaveConfiguration();
            _saved = true;
        }

        /// <summary>
        /// Saves the configuration.
        /// </summary>
        private void SaveConfiguration()
        {
            //check whether configurations are numeric
            bool isNumber = false;
            int res;
            isNumber = Int32.TryParse(this.tbx_capImgFreq.Text, out res);
            isNumber = Int32.TryParse(this.tbx_imgWidth.Text, out res);
            isNumber = Int32.TryParse(this.tbx_imgHeight.Text, out res);

            if (isNumber)
            {
                Configuration.CaptionImageFrequency = Convert.ToInt32(this.tbx_capImgFreq.Text);
                Configuration.ImageWidth = Convert.ToInt32(this.tbx_imgWidth.Text);
                Configuration.ImageHeight = Convert.ToInt32(this.tbx_imgHeight.Text);
                Configuration.UseOriginalSizes = cbx_useOriginals.Checked;
                Configuration.Language = selectionLanguage.Text;

                Configuration.SaveConfiguration();

                MessageBox.Show(this, Properties.Resources.text_ConfigurationSavedSuccessfully, Properties.Resources.mb_Info,
                    MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show(this, Properties.Resources.mb_ConfigurationCouldNotBeSaved, Properties.Resources.mb_Info,
                    MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void pb_infoCapImgFreq_MouseHover(object sender, EventArgs e)
        {
            ShowToolTip(pb_infoCapImgFreq, Properties.Resources.gh_capImageFreq, Properties.Resources.text_capImageFreqTooltip);
        }

        private void pb_infoImgSize_MouseHover(object sender, EventArgs e)
        {
            ShowToolTip(pb_infoImgSize, Properties.Resources.text_imageSize, Properties.Resources.text_ImageSizeToolTip);
        }

        private void ShowToolTip(Control ToolTipControl, string Caption, string Text)
        {
            toolTip.SetToolTip(ToolTipControl, Text);
            toolTip.ToolTipIcon = ToolTipIcon.Info;
            toolTip.UseAnimation = true;
            toolTip.UseFading = true;
            toolTip.IsBalloon = true;
            toolTip.AutoPopDelay = 10000; //10 seconds
            toolTip.ToolTipTitle = Caption;
            toolTip.Show(Text, this);
        }

        private void cbx_useOriginals_CheckedChanged(object sender, EventArgs e)
        {
            RetrieveImageSizesEnabled();

            if (!_isLoading)
                _saved = false;
        }

        private void RetrieveImageSizesEnabled()
        {
            if (cbx_useOriginals.Checked)
            {
                this.tbx_imgWidth.Enabled = false;
                this.tbx_imgHeight.Enabled = false;
            }
            else
            {
                this.tbx_imgWidth.Enabled = true;
                this.tbx_imgHeight.Enabled = true;
                this.tbx_imgWidth.ReadOnly = false;
                this.tbx_imgHeight.ReadOnly = false;
            }
        }

        private void Frm_Configuration_Load(object sender, EventArgs e)
        {
            this.tbx_capImgFreq.Text = Configuration.CaptionImageFrequency.ToString();
            this.tbx_imgWidth.Text = Configuration.ImageWidth.ToString();
            this.tbx_imgHeight.Text = Configuration.ImageHeight.ToString();
            this.cbx_useOriginals.Checked = Configuration.UseOriginalSizes;
            this.selectionLanguage.Text = Configuration.Language;

            //handle checkedstatus
            RetrieveImageSizesEnabled();

            _isLoading = false;
        }

        private void tbx_TextChanged(object sender, EventArgs e)
        {
            if (!_isLoading)
                _saved = false;
        }

        private void Frm_Configuration_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!_saved)
            {
                DialogResult dr = MessageBox.Show(this, Properties.Resources.text_notSavedChanges,
                    Properties.Resources.mb_Info, MessageBoxButtons.YesNo, MessageBoxIcon.Question);

                if (dr == DialogResult.No)
                {
                    e.Cancel = true;
                }
            }
        }

        private void selectionLanguage_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!_isLoading)
            {
                Configuration.Language = selectionLanguage.Text;
                DialogResult dr = MessageBox.Show(Properties.Resources.text_ApplicationRestart, Properties.Resources.mb_Info,
                    MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                Configuration.SaveConfiguration();

                if (dr == DialogResult.Yes)
                {
                    Application.Restart();
                }
            }
        }
    }
}
